package com.ls.customer.controller;

import com.ls.customer.entity.CustInter;
import com.ls.customer.entity.InterOrder;
import com.ls.customer.entity.InterPrice;
import com.ls.customer.feignclients.PayFeignClients;
import com.ls.customer.service.CustomerService;
import com.ls.customer.service.InterService;
import com.ls.customer.entity.Inter;
import com.ls.utils.Result;
import com.ls.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 钰杰
 * @className CustomerController
 * @description VX:FengYJ996
 * @date 2019/12/2519:38
 */
@RestController
@RequestMapping("/customercontroller")
//游客：0、2：个人用户、1：企业用户
public class CustomerController {


    @Autowired
    private CustomerService customerService;

    @Autowired
    private InterService interService;

    @Autowired
    private PayFeignClients payFeignClients;

    /**
     * 查看用户信息
     *
     * @param appid
     * @return
     */
    @RequestMapping(value = "/getCustomerInfoByappid", method = RequestMethod.GET)
    public Result getCustomerInfoByappid(String appid) {
        Map<String, Object> customerByAppid = customerService.getCustomerByAppid(appid);
        if (StringUtils.isEmpty(customerByAppid)) {  //查无此用户
            return new Result(false, ResultCode.CHECK_FOR_NO_SUCH_USER.getCode(),
                    ResultCode.CHECK_FOR_NO_SUCH_USER.getDesc(), null, "getCustomerByAppid");
        }
        return new Result(true, ResultCode.OK.getCode(), ResultCode.OK.getDesc(), customerByAppid, "getCustomerByAppid");
    }


    /**
     * 购买接口（等待管理员审核：0：未审核，1：通过，2：驳回）
     * 1.接口列表
     * 2.申请接口
     * 3.价格明细
     * 4.中间表审核
     */

    // 获取接口列表
    @RequestMapping(value = "/getAllInterface", method = RequestMethod.GET)
    public Result getAllInterface() {
        List<Inter> intersAll = interService.getIntersAll();
        return new Result(true, ResultCode.OK.getCode(), ResultCode.OK.getDesc(), intersAll, "getAllInterface");
    }

    //申请接口
    @RequestMapping(value = "/applyInter/{appid}/{interid}", method = RequestMethod.GET)
    public void applyInter(@PathVariable("appid") String appid,
                           @PathVariable("interid") String interid) {
        interService.applyInter(appid, interid);
    }

    //获取当前接口价格明细
    @RequestMapping(value = "/getInterPrice/{interfaceid}", method = RequestMethod.POST)
    public Result getInterPrice(@PathVariable("interfaceid") String interfaceid) {
        InterPrice interPriceByInterfaceid = interService.getInterPriceByInterfaceid(interfaceid);
        return new Result(true, ResultCode.OK.getCode(), ResultCode.OK.getDesc(), interPriceByInterfaceid);
    }

    //生成订单
    @Transactional
    @RequestMapping(value = "/addInterfaceToCustomer", method = RequestMethod.POST)
    public Result addInterfaceToCustomer(@RequestBody InterOrder interOrder) {
        //生成购买接口订单(默认1,待支付)
        InterOrder newInterOrder = interService.addInterOrder(interOrder);
        //支付订单信息
        ResultCode resultCode = customerService.payForOrder(newInterOrder.getInteroid());
        //支付成功生成中间表
        if (resultCode.getCode() == 0) {//支付成功
            customerService.addCustInter(newInterOrder);
            return new Result(true, resultCode.getCode(), resultCode.getDesc());
        } else {
            return new Result(false, resultCode.getCode(), resultCode.getDesc());
        }

    }


    //查看使用记录
    @RequestMapping(value = "/getUserRecord/{appid}", method = RequestMethod.GET)
    public Result getUserRecord(@PathVariable("appid") String appid) {
        List<CustInter> interlist = customerService.getInterlist(appid);
        return new Result(true, ResultCode.OK.getCode(), ResultCode.OK.getDesc(), interlist);
    }

    /**
     * 获取用户所属接口列表
     *
     * @return
     */
    @RequestMapping(value = "/getInterListByappid/{appid}", method = RequestMethod.GET)
    public Result getInterListByappid(@PathVariable("appid") String appid) {
        List<CustInter> interlist = customerService.getInterlist(appid);
        return new Result(true, ResultCode.OK.getCode(), ResultCode.OK.getDesc(), interlist);
    }


}
